package run;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;

import java.util.List;
import java.util.Map;

public class AutoCode {

  public static void main(String[] args) {
    Dict dict = Dict.create();
    dict.set("packageName", "com.bfmp.sdk.nuonuo.dto.sale");
    dict.set("className", "InvoicingReqDTO");

    List<Map<String,Object>> fields = CollUtil.newArrayList();
    dict.set("fields", fields);

    CsvReader reader = CsvUtil.getReader();
    reader.setFieldSeparator('|');
    reader.setContainsHeader(true);
    // 从文件中读取CSV数据
    CsvData data = reader.read(FileUtil.file("/home/xiuyuan/tmp/1.csv"));
    List<CsvRow> rows = data.getRows();
    // 遍历行
    for (CsvRow csvRow : rows) {
      String field = csvRow.get(1);
      if (StrUtil.isEmpty(field)){
        continue;
      }
      String type = "String";
      String require = csvRow.get(4);
      String des = csvRow.get(2);
      String example = csvRow.get(5);

      Map<String, Object> fieldMap = Dict.create();
      fieldMap.put("field", field);
      fieldMap.put("type", type);
      fieldMap.put("require", require);
      fieldMap.put("des", des);
      fieldMap.put("example", example);
      fields.add(fieldMap);
    }

    TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig());

    String template =
        """
     package ${packageName};

     import lombok.Data;
     import lombok.NoArgsConstructor;

     import java.io.Serializable;


     /**
      * 销项 开具发票 req DTO
      *
      * @author bfmp
      */
     @NoArgsConstructor
     @Data
     public class ${className} implements Serializable {

          <#list fields as field>
          /**
           * @see ${field.type!} ${field.des!}
           * 是否必填 ${field.require!}
           * 示例 ${field.example!}
           * ${field.ext!}
           */
          private ${field.type!} ${field.field!};
          </#list>

     }
     """;

    String render = engine.getTemplate(template).render(dict);
    System.out.println(render);
  }
}
